from model.company import Company


class CompanyDao:
    def __init__(self, session):
        self.session = session

    def add(self, company):
        self.session.add(company)

    def get_by_uuid(self, uuid):
        q = self.session.query(Company) \
            .filter(Company.c_uuid == uuid)
        return q.first()

    def merge(self, company):
        self.session.merge(company)

    def get_by_company_id(self, company_id):
        try:
            q = self.session.query(Company) \
                .filter(Company.company_id == company_id)
            if q:
                return q.first()
            else:
                return None
        except Exception as ex:
            raise ex;
            return None

    def get_by_company_id_detail(self, company_id):
        sql="""
        select
        a.company_id,a.name,
        a.address,a.note,a.tel,a.credentials_url,
        a.credentials_name,
        a.size,
        a.certification,
        province.`name` as province_name,
        city.`name` as city_name,
        country.`name` as country_name,
        a.province_code,a.city_code,a.country_code
        from
        company a
        left join
        province_city as province
        on a.province_code=province.`code`
        left join province_city as city
        on a.city_code=city.code
        left join province_city as country
        on a.country_code=country.code
        where a.company_id=:company_id
        """
        result = self.session.execute(sql,{"company_id":company_id})

        if result:
           for data in result:
             return {
                 "company_id":data.company_id,
                 "name": data.name if data.name else '',
                 "address": data.address if data.address else '',
                 "note": data.note if data.note else '',
                 "tel": data.tel,
                 "credentials_url": data.credentials_url,
                 "credentials_name": data.credentials_name,
                 "province_name": data.province_name if data.province_name else '',
                 "city_name": data.city_name  if data.city_name else '',
                 "country_name": data.country_name  if data.country_name else '',
                 "province_code": data.province_code ,
                 "city_code": data.city_code,
                 "country_code": data.country_code,
                 "size": data.size,
                 "certification": data.certification             }
             pass
        else:
             return None



    def get_by_name(self, c_name):
        try:
            q = self.session.query(Company).filter(Company.name == str(c_name))
            if q:
                return q.first()
            else:
                return None
        except Exception as ex:
            raise ex;
            return None


if __name__ == '__main__':
    # dd = {"company_id": 4, "name": 'ttt', 'title': '', 'sex': 1}
    # cmp = Company()
    # for (k, v) in dd.items():
    #     setattr(cmp, k, v)
    # print(cmp.company_id)
    pass
